let Father = require('./Father');
let model = require('../../model/base');
const { title } = require('process');
let jiami = require('./md5')
let moment = require('moment');

class Son extends Father {
    // async page() {
    //     let title = '注册页面';
    //     return this.nunjucks.render('register.html', { title: title });
    // }
    async register() {
        if (this.req.post['password'] === this.req.post['definepassword']) {
            // this.req.post['username']
            // console.log(this.req.post['username']);
            let psd = jiami.md5(this.req.post['password']);
            let date = moment().format('YYYY-MM-DD HH:mm:ss');
            let sql = 'insert into user(username,password,date) values(?,?,?)';
            let model1 = new model();
            let values = [this.req.post['username'], psd, date]
            await model1.select(sql, values)
            let title = '注册成功'
            return this.nunjucks.render('success.html', { title: title });

        } else {
            let script = "<script>alert('两次密码不一致')</script>"
            // console.log(this.nunjucks.render('success.html', { script: script }));
            return this.nunjucks.render('success.html', { script: script });
        }

    }
    async login() {
        let selectsql = 'select * from user where username = ? and password = ?';
        let model2 = new model();
        let password = jiami.md5(this.req.post['password']);
        let username = this.req.post['username'];
        // let password = this.req.post['password'];
        let values2 = [username, password]
        let result = await model2.select(selectsql, values2);
        this.res.session = JSON.stringify(result);
        console.log(result.length);
        if (result.length > 0) {
            let sucs = '登录成功'
            return this.nunjucks.render('success.html', { sucs: sucs });
        } else {
            let error = '用户不存在或密码错误'
            return this.nunjucks.render('success.html', { error: error });
        }
    }

}
module.exports = Son;